package com.ostrich.shopping.ui.user.views;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.ostrich.shopping.R;
import com.ostrich.shopping.base.BaseMvvmActivity;
import com.ostrich.shopping.databinding.ActivityRegisterBinding;
import com.ostrich.shopping.net.BaseResponseEntity;
import com.ostrich.shopping.ui.user.entity.UserEntity;
import com.ostrich.shopping.ui.user.viewmodel.RegisterViewModel;

public class RegisterActivity extends BaseMvvmActivity<ActivityRegisterBinding, RegisterViewModel> {
    public static final int RESULT_CODE = 102;
    @Override
    public void createViewModel() {
        viewModel = new ViewModelProvider(this).get(RegisterViewModel.class);
    }


    @Override
    protected void iniView() {

    }

    @Override
    public int getLayout() {
        return R.layout.activity_register;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {
        binding.btRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = binding.etUsername.getText().toString();
                String password = binding.etPassword.getText().toString();
                String phonenumber = binding.etPhonenumber.getText().toString();
                UserEntity userEntity = new UserEntity();
                userEntity.setNickname(username);
                userEntity.setPassword(password);
                userEntity.setPhoneNumber(phonenumber);
                viewModel.register(userEntity).observe(RegisterActivity.this, new Observer<BaseResponseEntity<UserEntity>>() {
                    @Override
                    public void onChanged(BaseResponseEntity<UserEntity> userEntityBaseResponseEntity) {
                        if(userEntityBaseResponseEntity.getCode() == 0){
                            ToastUtils.showShort("注册成功");
                            UserEntity user = userEntityBaseResponseEntity.getData();
                            //将
                            Intent intent = new Intent();
                            intent.putExtra("user",user);
                            setResult(RESULT_CODE,intent);
                            finish();





                        }else{
                            ToastUtils.showShort(userEntityBaseResponseEntity.getMsg());
                        }
                    }
                });


            }
        });

    }
}